﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AD.Contract.Models;
using AD.Contract;
using AD.DAL;
using EntityFramework.Extensions;
using AD.IBLL;
using HZT.Framework.Contract;
namespace AD.BLL
{
    public class PeriodService : IPeriodService
    {
        public Period GetModel(string id)
        {
            using (var dbcontext = new ADDbContext())
            {
                return dbcontext.Find<Period>(id);
            }
        }

        public IEnumerable<Period> GetList(CommonRequest request = null)
        {
            request = request ?? new CommonRequest();
            using (var dbContext = new ADDbContext())
            {
                IQueryable<Period> models = dbContext.Periods;

                if (!string.IsNullOrEmpty(request.Name))
                {
                    models = models.Where(u => u.Name.Contains(request.Name));
                }

                return models.OrderByDescending(u => u.Id).ToPagedList(request.PageIndex, request.PageSize);
            }
        }
        public void Save(Period model)
        {
            using (var dbcontext = new ADDbContext())
            {
                if (!string.IsNullOrEmpty(model.Id))
                {
                    dbcontext.Update<Period>(model);
                }
                else
                {
                    model.Id = Guid.NewGuid().ToString();
                    model.CreateTime = DateTime.Now;
                    model.EndTime = model.StartTime.AddMilliseconds(model.TimeSpan * 1000);
                    dbcontext.Insert<Period>(model);
                }
            }
        }
        public void Delete(List<string> ids)
        {
            using (var dbContext = new ADDbContext())
            {
                dbContext.Periods.Where(u => ids.Contains(u.Id)).Delete();
            }
        }
    }
}
